package com.leyou.controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.pojo.Goods;
import com.leyou.pojo.SearchRequest;
import com.leyou.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@Controller
public class SearchController {

    @Autowired
    SearchService searchService;

    /*
    分页查询商品
     */
    @PostMapping("page")
    public ResponseEntity<PageResult<Goods>> search(@RequestBody SearchRequest searchRequest){
         PageResult<Goods> pageResult=this.searchService.search(searchRequest);
         if(pageResult==null || CollectionUtils.isEmpty(pageResult.getItems())){
             return ResponseEntity.notFound().build();
         }
         return ResponseEntity.ok(pageResult);
    }
}
